﻿using UnityEngine;

public class FrameState
{
    public SpriteRenderer render;
    public FrameAnimation anim;
    public int frameRate;

    private float interval = 0;
    private int length = 0;
    private int currFrameIndex = 0;
    private float elapsedTime = 0;

    public void Start()
    {
        interval = 1.0f / frameRate;

        currFrameIndex = 0;

        length = anim.Sprites.Length;
        render.sprite = anim.Sprites[0];
    }

    public void Update()
    {
        if (length == 1) return;

        if (anim.Loop == false && currFrameIndex >= length - 1) return;

        elapsedTime += Time.deltaTime;

        if (elapsedTime >= interval)
        {
            currFrameIndex = (currFrameIndex + 1) % length;
            render.sprite = anim.Sprites[currFrameIndex];
            elapsedTime = 0;
        }
    }

    public void Stop()
    {

    }
}
